import { HTTPBody, HTTPController, HTTPMethod, HTTPMethodEnum, HTTPQuery, Inject } from "@eggjs/tegg";
import { ClientGetTokenUtils, ClientSendMsgUtils, BaseController } from "@/module/common";
import { GroupMesService } from "../service/GroupMesService";
import { AnyType } from "typings/app";

@HTTPController({
    path: "/api/v1"
})
export class GroupMesController extends BaseController {

    @Inject()
    groupMesService: GroupMesService

    @Inject()
    clientMsgUtils: ClientSendMsgUtils

    @Inject()
    clientGetTokenUtils: ClientGetTokenUtils

    /**
     * 发送自定义机器人群组消息
     * @param body 消息体
     * @returns 
     */
    @HTTPMethod({
        path: "/sendMes",
        method: HTTPMethodEnum.POST
    })
    async SendGroupMes(@HTTPBody() body: any) {
        const { type, content, postUrl } = body;
        return await this.groupMesService.SendGroupMes(postUrl, content, type);
    }

    @HTTPMethod({
        path: "/sendQQMes",
        method: HTTPMethodEnum.POST
    })
    async sendQQMes(@HTTPBody() body: any) {
        const { receiveId, appId, content, type, receiveType } = body;
        return await this.clientMsgUtils.SendAnyMsg(receiveId, appId, content, type, receiveType);
    }

    @HTTPMethod({
        path: "/anyQQMes",
        method: HTTPMethodEnum.POST
    })
    async almightyMes(@HTTPBody() body: AnyType) {
        const { messageId, type, appId, receiveType, userIds, isRemove, emojiType, reactionId } = body;
        return await this.clientMsgUtils.AlmightyMes(messageId, type, appId, receiveType, userIds, isRemove, emojiType, reactionId);
    }

    @HTTPMethod({
        path: "/operQQMes",
        method: HTTPMethodEnum.POST
    })
    async OperationQQMes(@HTTPBody() body: any) {
        const { messageId, type, appId, content, receiveType } = body;
        return await this.clientMsgUtils.OperationMes(messageId, appId, receiveType, content, type);
    }

    @HTTPMethod({
        path: "/appSecret",
        method: HTTPMethodEnum.POST
    })
    async InsertAppId(@HTTPBody() body: any) {
        const { appId, appSecret } = body;
        // let App = this.clientGetTokenUtils.cryptoUtils.encrypt(appId,Config.SECURITY.SECRET_KEY);
        // console.log(App);
        // "34db86fd2115a88882b2f7f1f7c3273f:950216d2bac4d1903466bdc9aaec76917076b58d5cf7692b768b9cd9dcf5bee1"
        // let Secret = this.clientGetTokenUtils.cryptoUtils.encrypt(appSecret,Config.SECURITY.SECRET_KEY);
        // console.log(Secret);
        // "4646bf6e8f360c5d3c8e6733de46a571:dc001b79dd6fb9540261623e775d0a8286d3ba7d141372e241f223516a2bb6d7be416dec3e41754b5d8ee017588042dc"
        await this.clientGetTokenUtils.InsertAppId(appId, appSecret);
        return this.success()
    }

    @HTTPMethod({
        path: "/delAppId",
        method: HTTPMethodEnum.POST
    })
    async DeleteAppId(@HTTPBody() body: any) {
        const { appId } = body;
        await this.clientGetTokenUtils.DeleteAppId(appId);
        return this.success()
    }

    @HTTPMethod({
        path: "/Token",
        method: HTTPMethodEnum.GET
    })
    appId2Token(@HTTPQuery() decrypted: string) {
        return this.clientGetTokenUtils.GetToken(decrypted);
    }

    @HTTPMethod({
        path: "/getAllTokens",
        method: HTTPMethodEnum.GET
    })
    AllTokens() {
        return this.clientGetTokenUtils.GetAllTokens();
    }

    @HTTPMethod({
        path: "/readDocsData",
        method: HTTPMethodEnum.POST
    })
    async readDocsData(@HTTPBody() body: { appToken: string, tableId: string, data?: any, appId?: string }) {
        const { appToken, tableId, data, appId } = body;
        return this.success(await this.clientMsgUtils.readDocsData(appToken, tableId, data, appId))
    }

    @HTTPMethod({
        path: "/insertDocsData",
        method: HTTPMethodEnum.POST
    })
    async insertDocsData(@HTTPBody() body: { appToken: string, tableId: string, data: any, appId?: string }) {
        const { appToken, tableId, data, appId } = body;
        if (data.records) {
            return this.success(await this.clientMsgUtils.insertDocsDatas(appToken, tableId, data, appId))
        } else {
            return this.success(await this.clientMsgUtils.insertDocsData(appToken, tableId, data, appId))
        }
    }

    @HTTPMethod({
        path: "/updateDocsData",
        method: HTTPMethodEnum.POST
    })
    async updateDocsData(@HTTPBody() body: { appToken: string, tableId: string, data: any, recordId: string, appId?: string }) {
        const { appToken, tableId, recordId, data, appId } = body;
        if (data.records) {
            return this.success(await this.clientMsgUtils.updateDocsDatas(appToken, tableId, data, appId))
        } else {
            return this.success(await this.clientMsgUtils.updateDocsData(appToken, tableId, recordId, data, appId))
        }
    }

    @HTTPMethod({
        path: "/deleteDocsData",
        method: HTTPMethodEnum.POST
    })
    async deleteDocsData(@HTTPBody() body: { appToken: string, tableId: string, data: any, recordId: string, appId?: string }) {
        const { appToken, tableId, recordId, data, appId } = body;
        if (!recordId) {
            return this.success(await this.clientMsgUtils.deleteDocsDatas(appToken, tableId, data, appId))
        } else {
            return this.success(await this.clientMsgUtils.deleteDocsData(appToken, tableId, recordId, appId))
        }
    }
}